//
//  CWCDetailReadingViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCDetailReadingViewController.h"

@interface CWCDetailReadingViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic, retain)NSMutableDictionary *readingDic;
@property(nonatomic, assign)BOOL isSave;

@end

@implementation CWCDetailReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
   self.navigationController.navigationBar.translucent = NO;
    [self creatView];
    
    
    
}


-(void)creatView{
    self.navigationController.navigationBar.translucent = NO;
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT )];
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    [self setupWebView];
        
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    
}
-(void)setupWebView{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html",self.id] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.readingDic = dic[[NSString stringWithFormat:@"%@",self.id]];
        NSString *htmlStr = [self.readingDic objectForKey:@"body"];
        NSString *titleStr = self.readingDic[@"title"];
        NSString *sourecStr = self.readingDic[@"source"];
        NSString *ptimeStr = self.readingDic[@"ptime"];
        NSString *ptime = [ptimeStr substringWithRange:NSMakeRange(5, 11)];
        NSString *digestStr = self.readingDic[@"digest"];
        NSArray *imageArray = self.readingDic[@"img"];
        for (int i = 0; i < imageArray.count; i++) {
            NSString *imgStr = [NSString stringWithFormat:@"<!--IMG#%d-->",i];
        NSString * imgUrl = [imageArray[i] valueForKey:@"src"];
        NSString * sizeStr = [imageArray[i] valueForKey:@"pixel"];
        NSString * altStr = [imageArray[i] valueForKey:@"alt"];
        float width = [[sizeStr componentsSeparatedByString:@"*"].firstObject floatValue];
        float height = [[sizeStr componentsSeparatedByString:@"*"].lastObject floatValue];
        float imgViewWidth =self.webView.frame.size.width-16;
        float imgViewHeight = imgViewWidth* height/width;
            
            
        NSString * replaceStr = [NSString stringWithFormat:@"<div><br><img src=\"%@\" alt = \"%@\" width=\"%@\" height=\"%@\"></br></div>",imgUrl,altStr,[NSString stringWithFormat:@"%f",imgViewWidth],[NSString stringWithFormat:@"%f",imgViewHeight]];
        NSString * altStr1 = [NSString stringWithFormat:@"<div style = \"font-size:15px;color:#707070;\">%@</div>",altStr];
        NSString * replaceStr1 = [replaceStr stringByAppendingString:altStr1];
            htmlStr = [htmlStr stringByReplacingOccurrencesOfString:imgStr  withString:replaceStr1];
        }
        [self.webView loadHTMLString:[NSString stringWithFormat:@"<div style = \"font-size:19px;color:#000000;font-weight:bold\">%@</div><div style = \"font-size:15px;color:#808080;\">%@\t\t\t%@</div></br><div style = \"font-size:15px;color:#808080;\">%@</div><div style = \"font-family:arial,sans-serif;margin: 0; font-weight:normal; word-wrap: break-word;-webkit-user-select: none;text-align:justify;letter-spacing:-0.2px;-webkit-hyphenate-limit-after:1;-webkit-hyphenate-limit-before:1;-webkit-hyphens: auto;\">%@</div><br></br>",titleStr,sourecStr,ptime,digestStr,htmlStr] baseURL:nil];
        self.webView.delegate = self;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];

}

-(void)click:(UIButton *)button{
    
    int result = [[DatabaseTool shareDatabaseTool]inserReading:self.reading];
    if (result == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"  style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"该文章已经被收藏!" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
-(void)dealloc{
    [_reading release];
    [_titleLabel release];
    [_sourceLabel release];
    [_ptimeLabel release];
    [_bodyTextView release];
    [_webView release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
